import urls from '/util/urls'
import request from '/util/request'
const app = getApp()

Page({
  data: {
    id: '',
    cards: [
      {'name':'服务前巡检','info':'','type':'before'},
      {'name':'每日上报','info':'','type':'report'},
      {'name':'服务后巡检','info':'','type':'after'},
      {'name':'服务总结','info':'','type':'summary'}
    ],
    beforeInspectionStatus:'',
    afterInspectionStatus:''
  },
  onLoad(options) {
    let that = this
    // 传递的参数
    const id = options.id
    // 加载每日巡检的详细信息
    request.get(urls.inspectionServiceTaskDetailUrl(id), function(data){
      const beforeInspectionStatus = data.beforeInspectionStatus
      let inspectionStatusText = ''
      if (beforeInspectionStatus === 'notActive') {
        inspectionStatusText = '未激活'
      } else if (beforeInspectionStatus === 'pending') {
        inspectionStatusText = '待处理'
      } else if (beforeInspectionStatus === 'completed') {
        inspectionStatusText = '已完成'
      }
      

      const afterInspectionStatus = data.afterInspectionStatus
      let afterInspectionStatusText = ''
      if (afterInspectionStatus === 'notActive') {
        afterInspectionStatusText = '未激活'
      } else if (afterInspectionStatus === 'pending') {
        afterInspectionStatusText = '待处理'
      } else if (afterInspectionStatus === 'completed') {
        afterInspectionStatusText = '已完成'
      }


      const summaryStatus = data.summaryStatus
      let summaryStatusText = ''
      if (summaryStatus === 'notActive') {
        summaryStatusText = '未激活'
      } else if (summaryStatus === 'pending') {
        summaryStatusText = '待处理'
      } else if (summaryStatus === 'completed') {
        summaryStatusText = '已完成'
      }


      that.setData({
        id:data.id,
        ['cards['+0+'].info'] :  inspectionStatusText,
        ['cards['+1+'].info'] :  data.completeReportedNum + '/' + data.totalReportedNum,
        ['cards['+2+'].info'] :  afterInspectionStatusText,
        ['cards['+3+'].info'] :  summaryStatusText,
        beforeInspectionStatus:beforeInspectionStatus,
        afterInspectionStatus:afterInspectionStatus,
        summaryStatus:summaryStatus
      })
    })
  },
  onItemClick(e) {
    const type = e.target.dataset.id
    if(type == 'before'){
      const beforeInspectionStatus = this.data.beforeInspectionStatus
      if(beforeInspectionStatus == 'pending' || beforeInspectionStatus == 'completed') {
        dd.redirectTo({
          url: '/pages/inspection/insurance/before/before?id=' + this.data.id
        })
      } else {
        dd.showToast({
          type: 'fail',
          content: '任务没有激活',
          duration: 2000,
        })
      }
    }
    else if (type == 'after'){
      const afterInspectionStatus = this.data.afterInspectionStatus
      if(afterInspectionStatus == 'pending' || afterInspectionStatus == 'completed') {
        dd.redirectTo({
          url: '/pages/inspection/insurance/after/after?id=' + this.data.id
          // url: '/pages/index/index'
        })
      } else {
        dd.showToast({
          type: 'fail',
          content: '任务没有激活',
          duration: 2000,
        })
      }
    }
    else if (type == 'report'){
      dd.redirectTo({
        url: '/pages/inspection/insurance/report/report?id=' + this.data.id
      })
    }else if(type == 'summary'){
      const summaryStatus = this.data.summaryStatus
       if(summaryStatus == 'pending' || summaryStatus == 'completed') {
        dd.redirectTo({
          url: '/pages/inspection/insurance/summary/summary?id=' + this.data.id
        })
      } else {
        dd.showToast({
          type: 'fail',
          content: '任务没有激活',
          duration: 2000,
        })
      }
    }
  }
});
